﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model;
using HengTong.Model.Db.Hr;


namespace HengTong.ViewModel.Person.SysPersonContractVms
{
    public partial class SysPersonContractListVm : BasePagedListVM<SysPersonContractView, SysPersonContractSearcher>
    {

        protected override IEnumerable<IGridColumn<SysPersonContractView>> InitGridHeader()
        {
            return new List<GridColumn<SysPersonContractView>>{
                this.MakeGridHeader(x => x.PersonName),
                this.MakeGridHeader(x => x.EmploymentType),
                this.MakeGridHeader(x => x.ContractType),
                this.MakeGridHeader(x => x.ContractNo),
                this.MakeGridHeader(x => x.SignDate),
                this.MakeGridHeader(x => x.ExpireDate),
                this.MakeGridHeader(x => x.ContractCount),
                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<SysPersonContractView> GetSearchQuery()
        {
            var query = DC.Set<SysPersonContract>()
                .CheckEqual(Searcher.PersonId, x=>x.PersonId)
                .CheckEqual(Searcher.EmploymentType, x=>x.EmploymentType)
                .CheckEqual(Searcher.ContractType, x=>x.ContractType)
                .CheckContain(Searcher.ContractNo, x=>x.ContractNo)
                .CheckBetween(Searcher.SignDate?.GetStartTime(), Searcher.SignDate?.GetEndTime(), x => x.SignDate, includeMax: false)
                .CheckBetween(Searcher.ExpireDate?.GetStartTime(), Searcher.ExpireDate?.GetEndTime(), x => x.ExpireDate, includeMax: false)
                .CheckEqual(Searcher.ContractCount, x=>x.ContractCount)
                .Select(x => new SysPersonContractView
                {
				    ID = x.ID,
                    PersonName = x.Person.Name,
                    EmploymentType = x.EmploymentType,
                    ContractType = x.ContractType,
                    ContractNo = x.ContractNo,
                    SignDate = x.SignDate,
                    ExpireDate = x.ExpireDate,
                    ContractCount = x.ContractCount,
                })
                .OrderBy(x => x.SignDate ?? DateTime.MaxValue);
            return query;
        }

    }

    public class SysPersonContractView : SysPersonContract{
        [Display(Name = "姓名")]
        public String PersonName { get; set; }

    }
}
